﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
    public class HoaDonDAO
    {
        //public static DataTable GetAll()
        //{
        //    string sql = "Select * From HoaDon";
        //    DataTable dt = DataProvider.ExecuteQuery(sql);
        //    return dt;
        //}
        public class SqlConnectionData
        {
            public static SqlConnection Connection()
            {
                SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True");
                return conn;
            }
        }

        public static DataTable GetAll()
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_HoaDonGetAll", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;

        }
        public static void InsertHoaDon(HoaDonDTO hd)
        {
            SqlConnection conn = SqlConnectionData.Connection();
            SqlCommand cmd = new SqlCommand("sp_InsertHoaDon", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaKhachHang", SqlDbType.Int);
            cmd.Parameters.Add("@NgayLap", SqlDbType.DateTime);
            //cmd.Parameters.Add("@TriGia", SqlDbType.Float);
            cmd.Parameters["@MaKhachHang"].Value = hd.MaKhachHang;
            cmd.Parameters["@NgayLap"].Value = hd.NgayLap;
            //cmd.Parameters["TriGia"].Value = null;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public static DataTable LayMaxMaHoaDon()
        {
            string sql = "select MAX(MaHoaDon) from HOADON";
            DataTable dt = DataProvider.ExecuteQuery(sql);
            return dt;
        }
        public static void CapNhatHoaDon(float tongtien, int mahd)
        {
            string sql = string.Format("Update HOADON Set TriGia=" + tongtien + " Where MaHoaDon = " + mahd + "");
            DataProvider.ExecuteNonQuery(sql);
        }

    }
}
